package ProjectViewer;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import ClassData.ClassData;
import ClassData.ClassDataList;
import DataFile.DataFileCreator;
import System.Settings_System;
import Uploader.Uploader;
import Uploader.Uploader.UploadResult;








public class Window_ProjectView extends JInternalFrame implements ActionListener, MenuListener {

	
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

ClassDataList myClassList; 

public static final String cmd_load="cmdLoad"; 
public static final String cmd_link ="cmdLink"; 
public static final String cmd_upload ="cmdUpload";
public static final String cmd_selectPath ="cmdSelectPath";
public static final String cmd_renameProject ="cmdRenameProject";
public static final String cmd_setPath ="cmdSetPath";
public static final String cmd_setBaseAddres ="cmdSetBaseAddres";


private static final String [] [] MessageName =
				{{"Package Information","Class","List","Explorer"},
				{"Projekt","Klasse","Liste","Explorer"}};	

private static final int String_WindowNameID					=0; 
private static final int String_SelectorID					=String_WindowNameID+1; 
private static final int String_ListID						= String_SelectorID+1;
private static final int String_ExplorerID					= String_ListID+1;

Panel_ClassList 	classListPanel;
Panel_ProjectData 	projectDataPanel;

Data_Project	    projectData;



private static final String [] menuNames  = {"File","Save","Save as..","Settings"}; 

private static final int menuName_fileID = 0;
private static final int menuName_saveID = menuName_fileID+1;
private static final int menuName_SaveAsID = menuName_saveID+1;
private static final int menuName_SettingseID = menuName_SaveAsID+1;



private static final String [] menuCommands ={"mCmd_Save","mCmd_SaveAs"};


public static final int mCmd_SaveID	=0;
public static final int mCmd_SaveAsID	=mCmd_SaveID+1;






private  JMenuBar projectMenuBar;



public Window_ProjectView()
{
	super(MessageName [Settings_System.GetActiveLanguage()] [String_WindowNameID],true,true,true,true);
	
	
	
	projectData= new Data_Project();
	projectData.setProjectName("new");
	projectData.setProjectPath("");
	projectData.setBasisAdress("0x0");
	
	
	setSize(1200,820);

	
	
	
	
	myClassList=new 	ClassDataList();
	
	
	setLayout(null);

	projectMenuBar=makeProjectMenu();
	projectMenuBar.setBounds(0, 0, 800, 20);
	add(projectMenuBar);

	
	
	JTabbedPane classSelectPanel;
	classSelectPanel = new JTabbedPane();
	
	
	
	classSelectPanel.setBorder(new TitledBorder(new LineBorder(Color.black,2),MessageName [Settings_System.GetActiveLanguage()] [String_SelectorID]));
	

	projectDataPanel= new Panel_ProjectData(this);
	projectDataPanel.setLocation(0,20);
	add(projectDataPanel);
	
	Panel_ClassData classDataPanel;
	
	classDataPanel= new Panel_ClassData();
	classDataPanel.setBounds(300,120,600,400);
	add(classDataPanel);
	
	
	

	
	classListPanel= new Panel_ClassList (classDataPanel);

//	Panel_ClassExplorer myClassExplorerPanel;
//	myClassExplorerPanel= new Panel_ClassExplorer ();

	Panel_PackageExplorer myPackageExplorerPanel;
	myPackageExplorerPanel = new Panel_PackageExplorer();
	
	classSelectPanel.addTab(MessageName [Settings_System.GetActiveLanguage()] [String_ListID], null,classListPanel, MessageName [Settings_System.GetActiveLanguage()] [String_ListID]);
//	classSelectPanel.addTab(MessageName [Settings_System.GetActiveLanguage()] [String_ExplorerID], null,myClassExplorerPanel, MessageName [Settings_System.GetActiveLanguage()] [String_ExplorerID]);

	
	classSelectPanel.setBounds(0,120,300,400);
	add(classSelectPanel);
	
	show();
	
}





private void loadClasses(File rootDirectory)
{
File FileList[];
int counter;
ClassData newClassData;



	if (rootDirectory.isDirectory())
		{
		FileList=rootDirectory.listFiles();
		
		for (counter=0;counter<FileList.length;counter++)
			{
			if (FileList[counter].isDirectory())
				{
				loadClasses(FileList[counter]);
				}
			else
				{
				if (FileList[counter].getName().endsWith(".class"))
					{
					newClassData=new ClassData(FileList[counter]);
					myClassList.add(newClassData);
					}
				}
			
			}
		
		}
	

//TODO Sort Classes	
//TODO Calculate inherited DataSizes		




}


/**
 * @author Tavin
 * 
 * date 2001.04.08
 * 
 * @return
 */

public JMenuBar makeProjectMenu()
{
	
	JMenuBar tmpMenuBar;
	tmpMenuBar= new JMenuBar();
	tmpMenuBar.add(MakeFileMenu());	
	
	return(tmpMenuBar);
}
/**
 * @author Tavin
 * 
 * date 2001.04.08
 * 
 * @param MenuText
 * @return
 */

public JMenu makeMenu(String MenuText)
{
JMenu TmpMenu;

TmpMenu = new JMenu(MenuText);
TmpMenu.addActionListener(this);
return(TmpMenu);
}


/**
 * @author Tavin
 * 
 * date 2001.04.08
 * 
 * @return
 */


JMenu MakeFileMenu()
{
JMenu	TmpMenu;

TmpMenu = new JMenu(menuNames [menuName_fileID]);
TmpMenu.add(MakeMenuItem(menuNames [menuName_saveID],menuCommands[mCmd_SaveID]));

TmpMenu.add(MakeMenuItem(menuNames [menuName_SaveAsID],menuCommands[mCmd_SaveAsID]));


return(TmpMenu);	
}

/**
 * @author Tavin
 * 
 * date 2001.04.08
 *  
 * @param MenuItemText
 * @param ActionCommand
 * @return
 */

JMenuItem MakeMenuItem(String MenuItemText,String ActionCommand)
{
JMenuItem TmpItem;

TmpItem = new JMenuItem(MenuItemText);
TmpItem.setActionCommand(ActionCommand);
TmpItem.addActionListener(this);
return(TmpItem);
}



@Override
public void actionPerformed(ActionEvent actionEvent) {
	// TODO Auto-generated method stub
	
	String actionCommand;
	
	actionCommand=actionEvent.getActionCommand();

	if (actionCommand.equals(cmd_load))
		{
		
		myClassList.clear();
		loadClasses(new File(projectDataPanel.getProjectPath()+"bin"));
		myClassList.parseDataList();
		
		classListPanel.setClassList(myClassList);
		
		
		}

	else if (actionCommand.equals(cmd_link))
		{
		DataFileCreator myFileCreator;
		myFileCreator=new DataFileCreator(myClassList,projectDataPanel.getProjectPath(),projectDataPanel.getProjectName());	
	
		}

	
	else if (actionCommand.equals(cmd_upload))
		{
	 	try {
        	File file = new File(projectDataPanel.getProjectPath()+"/"+projectDataPanel.getProjectName()+".bin");

        	FileInputStream fis = new FileInputStream(file);
        	byte[] data = new byte[fis.available()];
        	fis.read(data);
        	UploadResult result = Uploader.Upload(data, projectDataPanel.getSerialPort());
            if(result == UploadResult.SUCCESS){
            	JOptionPane.showMessageDialog(null, "Upload successfull!", "Success", JOptionPane.INFORMATION_MESSAGE);
            } else {
            	JOptionPane.showMessageDialog(null, "Upload failed!", "Error", JOptionPane.OK_OPTION);
            }
    	} catch (FileNotFoundException ex) {
			System.err.println("File not found!");
		} catch (IOException ex2) {
			System.err.println("IOException!");
		}
		}

	
	else if (actionCommand.equals(cmd_selectPath))
		{
		
		
		File projectPath;
		
		projectPath= new File(projectDataPanel.getProjectPath());
		
		JFileChooser MyFileChoser=new JFileChooser();
		MyFileChoser.setSelectedFile(projectPath);
		
		MyFileChoser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		

		MyFileChoser.showDialog(this,"select");

		projectDataPanel.setProjectPath(MyFileChoser.getSelectedFile().getAbsolutePath()+File.separator);
		}
	
	else if (actionCommand.equals(menuCommands[mCmd_SaveID]))
	{
		System.out.println("saving.....");
		projectData.save(projectDataPanel.getProjectPath()+projectDataPanel.getProjectName());
	}
	
	else if (actionCommand.equals(cmd_setBaseAddres))
		{
		projectDataPanel.getBaseAdress();

		}

	
	else if (actionCommand.equals(cmd_setPath))
		{
		projectDataPanel.getProjectPath();
	
		}

	
	System.out.println(actionCommand);
	
	
}





@Override
public void menuCanceled(MenuEvent arg0) {
	// TODO Auto-generated method stub
	
}





@Override
public void menuDeselected(MenuEvent arg0) {
	// TODO Auto-generated method stub
	
}





@Override
public void menuSelected(MenuEvent arg0) {
	// TODO Auto-generated method stub
	
}




}
